package com.czy.owner.ui.hotshop;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.adapter.HotShopClassifyLeftAdapter;
import com.czy.owner.adapter.StoreClassifyRightAdapter;
import com.czy.owner.api.HotShopClassifyApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.HotShopClassifyModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * Created by asus
 * 门店商品分类样式
 */
public class HotShopClassifyActivity extends BaseActivity {
    @BindView(R.id.rv_classify_left)
    RecyclerView rvClassifyLeft;
    @BindView(R.id.rv_classify_right)
    RecyclerView rvClassifyRight;
    private ArrayList<HotShopClassifyModel> classifyLeftModels = new ArrayList<>();
    private ArrayList<HotShopClassifyModel.SubsBean> classifyRightModels = new ArrayList<>();
    private HotShopClassifyLeftAdapter classifyLeftAdapter;
    private StoreClassifyRightAdapter classifyRightAdapter;
    private static int CLASSIFY_TYPE= 0;
    private static final int PLATFORM_TYPE = 0;
    private int goodsType = 0;
    private String goodsActivityId;


    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_hot_shop_classify;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        initData();
        initLeftClassify();
        initRightClassify();

    }

    private void initData() {
        goodsActivityId = getIntent().getStringExtra("goodsActivityId");
        goodsType = getIntent().getIntExtra("goodsType", PLATFORM_TYPE);
        String storeId = "0";
        if (goodsType != PLATFORM_TYPE) {
            storeId = SharedPreferencesUtils.getParam(HotShopClassifyActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "";
        }
        HotShopClassifyApi hotShopClassifyApi = new HotShopClassifyApi(new HttpOnNextListener<List<HotShopClassifyModel>>() {
            @Override
            public void onNext(List<HotShopClassifyModel> models) {
                setLeftAdapter(models);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<List<HotShopClassifyModel>>>() {
                }.getType();
                BaseResultEntity<List<HotShopClassifyModel>> cacheList = gson.fromJson(cache, type);
                setLeftAdapter(cacheList.getData());

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);

            }
        }, this, goodsType);
        hotShopClassifyApi.setSession(UserHelper.getInstance().getSession(this));
        hotShopClassifyApi.setStoreId(storeId);
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(hotShopClassifyApi);
    }


    private void initLeftClassify() {
        classifyRightModels.clear();
        classifyLeftAdapter = new HotShopClassifyLeftAdapter(this, 0);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvClassifyLeft.setAdapter(classifyLeftAdapter);
        rvClassifyLeft.setLayoutManager(linearLayoutManager);
        rvClassifyLeft.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, R.drawable.shape_recyclerview_divider_decoration));
        classifyLeftAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view,int position, Object o) {
                classifyLeftAdapter.updateSelectPosition(position);
                setRightAdapter(position);
            }
        });

    }

    private void initRightClassify() {
        classifyRightAdapter = new StoreClassifyRightAdapter(this);
        GridLayoutManager girdLayoutManager = new GridLayoutManager(this, 3);
        girdLayoutManager.setSpanSizeLookup(classifyRightAdapter.obtainTipSpanSizeLookUp());
        rvClassifyRight.setAdapter(classifyRightAdapter);
        rvClassifyRight.setLayoutManager(girdLayoutManager);
        classifyRightAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view,int position, Object o) {
                HotShopClassifyModel.SubsBean subsBean = (HotShopClassifyModel.SubsBean) o;
                Intent intent = new Intent(HotShopClassifyActivity.this, GoodsListActivity.class);
                intent.putExtra("fromType", CLASSIFY_TYPE);
                intent.putExtra("categoryId", subsBean.getCategoryId());
                intent.putExtra("goodsType", getIntent().getIntExtra("goodsType", PLATFORM_TYPE));
                intent.putExtra("title",subsBean.getCategoryName());
                intent.putExtra("mFromType", Constants.GOODSSOURCE_STORE);
                intent.putExtra("mFromTypeId", "0");
                if(!TextUtils.isEmpty(goodsActivityId)){
                    intent.putExtra("goodsActivityId",goodsActivityId);
                }
                startActivity(intent);
            }
        });


    }

    @Override
    protected void updateViews() {

    }

    @OnClick({R.id.img_back, R.id.ll_goods_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.ll_goods_search:
                Intent intent = new Intent(HotShopClassifyActivity.this, GoodsSearchHistoryActivity.class);
                intent.putExtra("goodsType", goodsType);
                startActivity(intent);
                break;
        }
    }

    private void setLeftAdapter(List<HotShopClassifyModel> models) {
        classifyLeftModels.clear();
        classifyLeftModels.addAll(models);
        classifyLeftAdapter.addAll(classifyLeftModels);
        if (models.size() > 0)
            setRightAdapter(0);
    }

    private void setRightAdapter(int position) {
        classifyRightModels.clear();
        classifyRightAdapter.clear();
        List<HotShopClassifyModel.SubsBean> subs = classifyLeftModels.get(position).getSubs();//二级
        for (int j = 0; j < subs.size(); j++) {
            HotShopClassifyModel.SubsBean subsBean = subs.get(j);
            subsBean.setGroup(true);
            classifyRightModels.add(subsBean);
            for (int k = 0; k < subsBean.getSubs().size(); k++) {
                subsBean.getSubs().get(k).setGroup(false);
                classifyRightModels.add(subsBean.getSubs().get(k));
            }

        }

        classifyRightAdapter.addAll(classifyRightModels);
    }

}
